'use strict';
const { DataTypes } = require('sequelize');
const sequelize = require('../config/db');
const PunchRecord = require('./PunchRecord');

const PunchPlan = sequelize.define('punch_plan', {
  userId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '所属用户ID'
  },
  name: {
    type: DataTypes.STRING(10),
    allowNull: false,
    validate: {
      notEmpty: {
        msg: '打卡名称不能为空'
      },
      len: {
        args: [1, 10],
        msg: '打卡名称字数不能超过10个字'
      }
    },
    comment: '打卡名称，必填，字数不超过10个字'
  },
  days: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    validate: {
      min: 0
    },
    comment: '打卡天数，必填，默认0，0表示不限制天数'
  },
  desc: {
    type: DataTypes.STRING(200),
    allowNull: true,
    validate: {
      len: {
        args: [0, 200],
        msg: '详细描述字数不能超过200字'
      }
    },
    comment: '详细的文字描述，非必填，限制200字'
  },
  punchedCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'punched_count',
    defaultValue: 0,
    comment: '已打卡次数'
  }
}, {
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

// 添加associate方法，而不是直接定义关联
PunchPlan.associate = function(models) {
  PunchPlan.hasMany(models.PunchRecord, { foreignKey: 'punchPlanId', onDelete: 'CASCADE' });
};

module.exports = PunchPlan;
